package View;
import java.util.ArrayList;

import BO.Category;
import BO.Item;
import BO.Order;
import BO.OrderItem;
import BO.User;
import DB.OrderDB;


public class Controller {
	
	private Controller(){
		
	}
	/**
	 * Get the category name.
	 * @param nr - The category number you like to request a name for.
	 * @return - The categorys name.
	 */
	public static String getCategoryNameByNr(int nr){
		return Category.getCategoryNameByNr(nr);
	}
	
	/**
	 * Add a new Category.
	 * @param name - The name of the new Category.
	 * @param parentId - The id for the new category's parent.
	 */
	public static void addCategory(String name, int parentId){
		Category.addCategory(name, parentId);
	}
	
	/**
	 * Edit a category.
	 * @param categoryId - The category's id.
	 * @param name - The category's new name.
	 */
	public static void editCategory(int categoryId, String name){
		Category.editCategory(categoryId, name);
	}
	
	/**
	 * Remove a category. All the category's children will get the category's parent as parent.
	 * But only if the category got a parent, will fail if the category don't have a parent.
	 * @param id - Id of the category to be removed.
	 * @return - True of false, depending on if the remove was successful.
	 */
	public static boolean removeCategory(int id){
		return Category.removeCategory(id);
	}	
	
	/**
	 * Get the complete category tree, with all it's relationships.
	 * @return - All categorys and there relationship.
	 */
	public static ArrayList<CategoryView> getCategoryTree(){
		ArrayList<Category> category = Category.getCategoryTree();
		ArrayList<CategoryView> view = new ArrayList<CategoryView>();
		for(int i = 0; i < category.size(); i++){
			view.add(new CategoryView(category.get(i)));
		}
		return view;
	}
	
	/**
	 * Get all the subcategories to a category.
	 * @param name - Name of the parent category.
	 * @return - All the category's children.
	 */
	public static ArrayList<CategoryView> getSubCategory(String name){
		ArrayList<Category> category = Category.getSubCategory(name);
		ArrayList<CategoryView> view = new ArrayList<CategoryView>();
		for(int i = 0; i < category.size(); i++){
			view.add(new CategoryView(category.get(i)));
		}
		return view;
	}
	
	/**
	 * Creates a orderItem containing an item and the amount you like to buy.
	 * @param id - Id of the item.
	 * @param amount - The amount you like to buy.
	 * @return - A OrderItemView containing a ItemView and an amount.
	 */
	public static OrderItemView getOrderItem(int id, int amount){
		Item item = Item.getItemById(id);
		if(item != null){
			OrderItem order = new OrderItem(item,amount);
			return new OrderItemView(order);
		}
		return null;
	}
	
	/**
	 * Get all the items under a specified category.
	 * @param categoryName - The category to look for items under.
	 * @return - All the items under the category.
	 */
	public static ArrayList<ItemView> getItemsByCategoryName(String categoryName){ 
		ArrayList<Item> item = Item.getItemsByCategoryName(categoryName);
		ArrayList<ItemView> view = new ArrayList<ItemView>();
		for(int i = 0; i < item.size(); i++){
			view.add(new ItemView(item.get(i)));
		}
		return view;
	}

	/**
	 * Update a item.
	 * @param id - The items id that will be updated.
	 * @param category - The new category.
	 * @param name - The new name.
	 * @param price - The new price.
	 * @param description - The new description.
	 * @param amount - The new amount in stock.
	 */
	public static void updateItem(int id, int category, String name, float price, String description, int amount){
		Item.updateItem(id, category, name, price, description, amount);
	}
	
	/**
	 * Get all information about an item.
	 * @param id - The id to get info about.
	 * @return - The available info about the item.
	 */
	public static ItemView getItemById(int id){
		return new ItemView(Item.getItemById(id));
	}
	
	/**
	 * Create a new item.
	 * @param category - The category the item will be found under.
	 * @param name - The name of the item.
	 * @param price - The price of the item.
	 * @param description - The item description.
	 * @param amount - The amount of the item you got in stock.
	 */
	public static void createItem(int category, String name, float price, String description, int amount){
		Item.createItem(category,name,price,description,amount);
	}
	
	/**
	 * Try to login a user.
	 * @param username - Username.
	 * @param password - Password.
	 * @return - If login passed or not.
	 */
	public static UserView login(String username, String password){
		return new UserView(User.login(username, password));
	}

	/**
	 * Edit a user.
	 * @param id - The user id that will be edited.
	 * @param username - The new username.
	 * @param rank - The new permission rank.
	 * @param address - The new address.
	 * @param zipcode - The new zipCode.
	 * @param city - The new city.
	 * @param email - The new email.
	 * @param firstname - The new first name.
	 * @param surname - The new surname.
	 */
	public static void editUser(int id, String username, int rank, String address, String zipcode, String city, String email, String firstname, String surname){
		User.editUser(id, username, rank, address, zipcode, city, email, firstname, surname);
	}
	
	/**
	 * Get user info from username.
	 * @param username - The username to get more information about.
	 * @return - All the available info about the user.
	 */
	public static UserView getUserByUsername(String username){
		return new UserView(User.getUserByUsername(username));
	}
	
	/**
	 * Create a new user, will only work if username is unique.
	 * @param username - Username.
	 * @param password - Password.
	 * @param rank - Permission rank.
	 * @param address - Address.
	 * @param zipcode - ZipCode.
	 * @param city - City.
	 * @param firstname - First name.
	 * @param surname - Surname.
	 * @param email - Email.
	 * @return If it was successful to add the user.
	 */
	public static boolean createUser(String username,String password, int rank, String address, String zipcode, String city, String firstname, String surname, String email){
		return User.createUser(username,password,rank,address,zipcode,city,firstname,surname,email);
	}
	
	/**
	 * Check if a item already exist in the shopping cart, if it does update the amount.
	 * @param cart - The cart to check.
	 * @param newItem - The item to be added.
	 * @return A new ArrayList with the added item.
	 */
	public static ArrayList<OrderItemView> checkCart(ArrayList<OrderItemView> cart, OrderItemView newItem){
		boolean found = false;
		for(int i = 0; i < cart.size(); i++){
			OrderItemView order = cart.get(i);
			if(order.getItem().getItemId() == newItem.getItem().getItemId()){
				order.increaseAmount(newItem.getAmount());
				found = true;
				break;
			}
		}
		if(!found)
			cart.add(newItem);
		return cart;
	}
	
	/**
	 * Update the cart (remove all items with an amount of zero or less)
	 * @param cart - The cart to be updated.
	 * @return A new ArrayList with the updated cart.
	 */
	public static ArrayList<OrderItemView> updateCart(ArrayList<OrderItemView> cart){
		ArrayList<OrderItemView> list = new ArrayList<OrderItemView>();
		for(int i = 0; i < cart.size(); i++){
			if(cart.get(i).getAmount() > 0){
				list.add(cart.get(i));
			}
		}
		return list;
	}
	/**
	 * Get the price of all items in the cart.
	 * @param orders - The items in the cart.
	 * @return - The total price.
	 */
	public static float getTotalPriceOfCart(ArrayList<OrderItemView> orders){
		float price = 0;
		for(int i = 0; i < orders.size(); i++){
			OrderItemView order = orders.get(i);
			price += (order.getItem().getPrice() * order.getAmount());
		}
		return price;
	}
	
	/**
	 * Place an order for some items, only works if all items exist and the amount is in stock.
	 * @param orders - Order to be placed.
	 * @param user - The user that will place the order.
	 * @return - If the placing of orders was successful.
	 */
	public static boolean placeAnOrder(ArrayList<OrderItemView> orders, UserView user){
		ArrayList<OrderItemView> list = new ArrayList<OrderItemView>();
		for(int i = 0; i < orders.size(); i++){
			if(orders.get(i).getAmount() > 0){
				list.add(orders.get(i));
			}
		}
		return OrderItem.orderStuffs(list, user.getUserId());
	}
	
	/**
	 * Update an order to packed.
	 * @param orderId - Order id for the order to be updated.
	 */
	public static void packOrder(int orderId){
		OrderItem.packOrder(orderId);
	}
	
	/**
	 * Get all the orders that havn't been packed yet.
	 * @return
	 */
	public static ArrayList<OrderView> getAllUnpackedOrders(){
		ArrayList<Order> order = OrderItem.getAllUnpackedOrders();
		ArrayList<OrderView> view = new ArrayList<OrderView>();
		for(int i = 0; i < order.size(); i++){
			view.add(new OrderView(order.get(i)));
		}
		return view;
	}
	
	/**
	 * Get all the available order from a user.
	 * @param userId - The user id to get orders from.
	 * @return - The order the user have made.
	 */
	public static ArrayList<OrderView> getAllOrderFromUser(int userId){
		ArrayList<Order> order = OrderItem.getAllOrderFromUser(userId);
		ArrayList<OrderView> view = new ArrayList<OrderView>();
		for(int i = 0; i < order.size(); i++){
			view.add(new OrderView(order.get(i)));
		}
		return view;
	}

}
